package com.ddyx.service.impl;

import com.ddyx.entity.EventClickFavorite;
import com.ddyx.entity.Goods;
import com.ddyx.entity.user.UStore;
import com.ddyx.mapper.CartMapper;
import com.ddyx.mapper.EventClickFavoriteMapper;
import com.ddyx.mapper.EventClickPageviewMapper;
import com.ddyx.service.ICartService;
import com.ddyx.util.MQUtil;
import com.ddyx.util.MyBeanUtils;
import com.ddyx.util.RequestForm;
import com.ddyx.util.ResponseForm;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.jms.Destination;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class CartServiceImpl implements ICartService {
	@Resource
	EventClickPageviewMapper ecpMapper;
	@Resource
	EventClickFavoriteMapper ecfMapper;

	@Resource
	Destination topicDestination;

	@Resource
	JmsTemplate jmsTemplate;

	@Autowired
	private CartMapper cartMapper;

	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm CartPage(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (param.getData() != null) {
			try {
				Map<String, Object> paramMap = (Map<String, Object>) param.getData();
				List<UStore> stores = ecpMapper.getViewStore(paramMap);
				
				for (int i = 0; i < stores.size(); i++) {
					paramMap.put("storeNo", stores.get(i).getStoreNo());
					List<Goods> goods = ecfMapper.getCartGoods(paramMap);
					if (goods.size()>0) {
						stores.get(i).setGoodsList(goods);
					}else {
						stores.remove(i);
						i--;
					}
				}
				result.setData(stores);
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
		}
		return result;
	}
	
	
	
	/**
	 * 批量加入购物车
	 */
	@Transactional
	@Override
	public ResponseForm batchCart(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if(null!=param.getData()) {
		try {
			Map paramMap = (Map) param.getData();
			Date date = new Date();
			EventClickFavorite ecf = new EventClickFavorite();
			MyBeanUtils.populate(ecf, paramMap);
			ecf.setEventTime(date);
			
			List<Map> goods = (List<Map>) paramMap.get("goods");
			for (Map m : goods) {
				MyBeanUtils.populate(ecf, m);
				ecfMapper.insertSelective(ecf);
				ecf.setFavoriteEventId(null);//id置 空,不然主键冲突
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("参数有误");
			log.error("参数有误");
		}
		}
		return result;
	}



	@Override
	public ResponseForm getCart(RequestForm param) {
		ResponseForm result = new ResponseForm();
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		try {
			String wxUid = (String) paramMap.get("wxUid");
			String storeNo = (String) paramMap.get("storeNo");
			if (!StringUtils.isNotBlank(storeNo) || !StringUtils.isNotBlank(wxUid)) {
				result.setStatus(false);
				result.setMessage("请检查参数");
				log.error("请检查参数");
				return result;
			}

			List<Map<String, Object>> list = cartMapper.getCartByUserIdAndStoreNo(paramMap);
			result.setData(list);
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作错误");
			log.error("操作错误");
		}
		return result;
	}

	@Override
	public ResponseForm insertOrUpdateGoodsToCart(RequestForm param) {
		ResponseForm result = new ResponseForm();
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		try {
			String wxUid = (String) paramMap.get("wxUid");
			String storeNo = (String) paramMap.get("storeNo");
			String goodsId = (String) paramMap.get("goodsId");
			String goodsNum = (String) paramMap.get("goodsNum");
			String goodsPrice = (String) paramMap.get("goodsPrice");
			String specsName = (String) paramMap.get("specsName");
			String goodsPic = (String) paramMap.get("goodsPic");
			String goodsName = (String) paramMap.get("goodsName");
			String goodsColor = (String) paramMap.get("goodsColor");
			String originalPrice = (String) paramMap.get("originalPrice");
			String employeNo = (String) paramMap.get("employeNo");
			if (!StringUtils.isNotBlank(wxUid)
					|| !StringUtils.isNotBlank(storeNo)
					|| !StringUtils.isNotBlank(goodsId)
					|| !StringUtils.isNotBlank(goodsNum)
					|| !StringUtils.isNotBlank(goodsPrice)
					|| !StringUtils.isNotBlank(specsName)
					|| !StringUtils.isNotBlank(goodsName)
					|| !StringUtils.isNotBlank(goodsColor)
					|| !StringUtils.isNotBlank(originalPrice)
					|| !StringUtils.isNotBlank(goodsPic)
					|| !StringUtils.isNotBlank(employeNo)) {
				result.setStatus(false);
				result.setMessage("请检查参数");
				log.error("请检查参数");
				return result;
			}

			Map<String, Object> goodsInCart = cartMapper.getGoodsInCart(paramMap);
			if (goodsInCart != null) {
				goodsNum = String.valueOf(Integer.parseInt(String.valueOf(goodsInCart.get("goods_num"))) + Integer.parseInt(goodsNum));
				paramMap.put("goodsNum", goodsNum);
			}
			cartMapper.insertOrUpdateGoodsToCart(paramMap);

			//MQ
			MQUtil.sendObMsg(paramMap,jmsTemplate,topicDestination);
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作错误");
			log.error("操作错误");
		}
		return result;
	}

	@Transactional
	@Override
	public ResponseForm modifyGoodsInCart(RequestForm param) {
		ResponseForm result = new ResponseForm();
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		try {
			String wxUid = (String) paramMap.get("wxUid");
			String storeNo = (String) paramMap.get("storeNo");
			String goodsId = (String) paramMap.get("goodsId");
			String goodsNum = (String) paramMap.get("goodsNum");
			String goodsPrice = (String) paramMap.get("goodsPrice");
			String originalPrice = (String) paramMap.get("originalPrice");
			String specsName = (String) paramMap.get("specsName");
			String goodsPic = (String) paramMap.get("goodsPic");
			String goodsName = (String) paramMap.get("goodsName");
			String goodsColor = (String) paramMap.get("goodsColor");
			String oldGoodsId = (String) paramMap.get("oldGoodsId");
			String oldSpecsName = (String) paramMap.get("oldSpecsName");
			if (!StringUtils.isNotBlank(wxUid)
					|| !StringUtils.isNotBlank(storeNo)
					|| !StringUtils.isNotBlank(goodsId)
					|| !StringUtils.isNotBlank(goodsNum)
					|| !StringUtils.isNotBlank(goodsPrice)
					|| !StringUtils.isNotBlank(originalPrice)
					|| !StringUtils.isNotBlank(specsName)
					|| !StringUtils.isNotBlank(goodsName)
					|| !StringUtils.isNotBlank(goodsColor)
					|| !StringUtils.isNotBlank(goodsPic)
					|| !StringUtils.isNotBlank(oldGoodsId)
					|| !StringUtils.isNotBlank(oldSpecsName)) {
				result.setStatus(false);
				result.setMessage("请检查参数");
				log.error("请检查参数");
				return result;
			}


			if (oldGoodsId.equals(goodsId) && oldSpecsName.equals(specsName)) {
				//如果则只修改数量
				cartMapper.updateOldGoodsInCart(paramMap);
			} else {
				Map<String, Object> newCartDetail = cartMapper.getCartByUserIdStoreNoGoodsIdSpecsName(paramMap);
				Map<String, Object> oldCartMap = new HashMap<>(paramMap);
				oldCartMap.put("specsName", oldSpecsName);
				Map<String, Object> oldCartDetail = cartMapper.getCartByUserIdStoreNoGoodsIdSpecsName(oldCartMap);

				//如果把购物车的商品A换为新商品B且B不存在购物车内
				if (newCartDetail == null) {
					//如果购物车中A的数量小于等于要换的数量，则新建B，并删除A
					paramMap.put("goodsNum", Integer.parseInt(goodsNum));
					cartMapper.insertOrUpdateGoodsToCart(paramMap);
					paramMap.put("goodsId", oldGoodsId);
					paramMap.put("specsName", oldSpecsName);
					cartMapper.deleteGoodsInCart(paramMap);
				}

				//如果把购物车的商品A换为新商品B且B已存在购物车内
				if (newCartDetail != null) {
					//如果购物车中A的数量大于要换的数量，则更新A和B的数量
					if (Integer.parseInt(String.valueOf(oldCartDetail.get("goodsNum"))) > Integer.parseInt(goodsNum)) {
						paramMap.put("goodsNum", (int) newCartDetail.get("goodsNum") + Integer.parseInt(goodsNum));
						oldCartMap.put("goodsNum", Integer.parseInt(String.valueOf(oldCartDetail.get("goodsNum"))) - Integer.parseInt(goodsNum));
						cartMapper.updateGoodsInCart(paramMap);
						cartMapper.updateGoodsInCart(oldCartMap);
					} else {
						//如果购物车中A的数量小于等于要换的数量，则删除A并更新B的数量和价格
						paramMap.put("goodsId", oldGoodsId);
						paramMap.put("specsName", oldSpecsName);
						cartMapper.deleteGoodsInCart(paramMap);
						int newGoodsNum = (int) newCartDetail.get("goodsNum") + Integer.valueOf(goodsNum);
						BigDecimal newGoodsPrice = BigDecimal.valueOf(Long.parseLong(goodsPrice));
						paramMap.put("goodsNum", newGoodsNum);
						paramMap.put("goodsPrice", newGoodsPrice);
						paramMap.put("originalPrice", originalPrice);
						paramMap.put("goodsId", goodsId);
						paramMap.put("specsName", specsName);
						cartMapper.updateGoodsInCart(paramMap);
					}
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作错误");
			log.error("操作错误");
		}
		return result;
	}

	@Override
	public ResponseForm removeGoodsInCart(RequestForm param) {
		ResponseForm result = new ResponseForm();
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		try {
			String wxUid = (String) paramMap.get("wxUid");
			String storeNo = (String) paramMap.get("storeNo");
			String goodsId = (String) paramMap.get("goodsId");
			String specsName = (String) paramMap.get("specsName");
			if (!StringUtils.isNotBlank(storeNo)
					|| !StringUtils.isNotBlank(wxUid)
					|| !StringUtils.isNotBlank(goodsId)
					|| !StringUtils.isNotBlank(specsName)) {
				result.setStatus(false);
				result.setMessage("请检查参数");
				log.error("请检查参数");
				return result;
			}

			cartMapper.deleteGoodsInCart(paramMap);
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作错误");
			log.error("操作错误");
		}
		return result;
	}

	@Override
	public ResponseForm emptyCart(RequestForm param) {
		ResponseForm result = new ResponseForm();
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		try {
			String wxUid = (String) paramMap.get("wxUid");
			String storeNo = (String) paramMap.get("storeNo");
			if (!StringUtils.isNotBlank(storeNo) || !StringUtils.isNotBlank(wxUid)) {
				result.setStatus(false);
				result.setMessage("请检查参数");
				log.error("请检查参数");
				return result;
			}

			cartMapper.emptyCart(paramMap);
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作错误");
			log.error("操作错误");
		}
		return result;
	}

}
